package trieMatch.keywordMatcher;

// This is unpublished source code. Michah Lerner 2006

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import trieMatch.Interfaces.Similarity;

/**
 * Heuristic scoring by word and string comparisons, considering word order and size. Hand tuned.
 * 
 * @author Michah.Lerner
 * 
 */

public class ResultScorer_cos implements Similarity {

	/**
	 * Subjective quality score between two structures
	 * 
	 * @param other
	 *            Structure to compare with <code>this</code>
	 * @return subjective quality score
	 */

	public float sim(final StructuredElement element, final StructuredElement other) {
		final ArrayList<String> a1 = new ArrayList<String>(element.size());
		final ArrayList<String> a2 = new ArrayList<String>(element.size());
		for (final Atom a : element) {
			a1.add(a.getValue());
		}
		for (final Atom a : other) {
			a2.add(a.getValue());
		}
		final float res = sim(a1, a2);
		return res;
	}

	public float sim(final StructuredElement element, final String other) {
		final List<String> a1 = new ArrayList<String>(element.size());
		for (final Atom a : element) {
			a1.add(a.getValue());
		}
		final float res = sim(a1, Arrays.asList(other.toLowerCase().split("[^A-Za-z0-9]")));
		return res;
	}

	public float sim(final List<String> l1, final List<String> l2) {
		final Set<String> set = new HashSet<String>(l1);
		int both = 0;
		for (final String s : l2) {
			if (set.contains(s)) {
				both++;
			}
		}
		set.addAll(l2);
		return ((float) both) / ((float) (set.size()));
	}

}
